<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class pro_tagihan_listrik extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('pro_tagihan_listrik_model', 'Tagihan');
		//$this->load->model('pro_dt_tagihan_listrik_model', 'Detail');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Data Tagihan";
		viewByTemplate('pro_tagihan_listrik/index', $data, 'grid');
	}
	 
	function getdata()
	{
		$this->secure_session();
		echo $this->Tagihan->GetJSON();
	}

	function getdata2()
	{
		$this->secure_session();
		echo $this->Tagihan->GetJSON2();
	}

	/*function dtl_sample2_data()
	{
		// Ajax Response For Datagrid
		echo $this->Detail->GetJSON(post('no_detail_tagihan'));
	}*/

	function create()
	{
		// Display Form Create
		$data['title'] = "Tagihan Baru";
		$data['data'] = NULL;
		//$data['arr_reference']	= $this->Combo->SampleReference();
		$data['mode'] = "create";
		$query = $this->db->query("SELECT no_tagihan_listrik FROM (SELECT * FROM pro_tagihan_listrik ORDER BY no_tagihan_listrik DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		if($getquery)
			$MaxID = $getquery['no_tagihan_listrik'];
		else
			$MaxID = "TL0000000";

		$NextID = (int) substr($MaxID,2,7);

		$NextID++;
		
		$data['no_tagihan_listrik'] = "TL" . sprintf("%07s",$NextID);

		$query1 = $this->db->query("SELECT id_cabang FROM (SELECT * FROM pro_setup_cabang ORDER BY id_cabang DESC) WHERE rownum <=1");
	 				if ($query1->row_array()) {
	 					$getquery1 = $query1->row_array();
	 					$rowcount1 = $getquery1['id_cabang'];
	 					$autonum1 = $rowcount1 + 1;
	 				} else {
					$autonum1 = 1;
				}
		$data['id_tagihan_listrik_auto'] = $autonum1;
		viewByTemplate('pro_tagihan_listrik/form', $data, 'form');
	}

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Ubah Data Tagihan";
		$data['data'] = $this->Tagihan->GetSingleView(get('no_tagihan_listrik'));

		$no_tagihan_listrik = get('no_tagihan_listrik');
		$query = $this->db->query("SELECT tanggal FROM pro_tagihan_listrik WHERE no_tagihan_listrik = '".$no_tagihan_listrik."'");
        if ($query->row_array()) {
        $getquery = $query->row_array();
        $key1 = $getquery['tanggal'];
        }
		$key = explode("-" , $key1);

		$data['bulan'] = $key[0];
		$data['tahun'] = $key[1];
		
		$data['id_tagihan_listrik_edit'] = get('id_tagihan_listrik');
		$data['id_lokasi_edit'] = get('id_lokasi');
		$data['no_tenant_edit'] = get('no_tenant');
		// Define Combo Data
		$data['mode'] = "edit";
		viewByTemplate('pro_tagihan_listrik/form', $data, 'form');
	}
	
	function view()
	{
		// Display Form View
		$data['title'] = "View Data Tagihan";
		$data['data'] = $this->Tagihan->GetSingleView(get('no_tagihan_listrik'));
		
		$data['mode'] = "view";
		viewByTemplate('pro_tagihan_listrik/form', $data, 'form');
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");
		// Validation
		
		if (post("no_tagihan_listrik") == '') {
			$error->Add("no_tagihan_listrik", "No Tagihan Listrik", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM pro_tagihan_listrik WHERE no_tagihan_listrik = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("no_tagihan_listrik"))))."'")) > 0) {
			$error->Add("no_tagihan_listrik", "No Tagihan Listrik", $tpl_duplicate);
		}else if (post("nama_tenant") == '') {
			$error->Add("nama_tenant", "Nama Tenant", $tpl_empty);
		}else if (post("no_toko") == '') {
			$error->Add("no_toko", "No toko", $tpl_empty);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	=  array(
					'no_tagihan_listrik' => $this->input->post('no_tagihan_listrik'),
					'id_tagihan_listrik' => $this->input->post('id_tagihan_listrik'),
					'id_lokasi' => $this->input->post('id_lokasi'),
					'no_tenant' => $this->input->post('no_tenant'),
					'nama_tenant' => $this->input->post('nama_tenant'),
					'no_toko' => $this->input->post('no_toko'),
					'jenis_usaha' => $this->input->post('jenis_usaha'),
					'lokasi_tempat' => $this->input->post('lokasi_tempat'),
					'tanggal' => $this->input->post('tanggal'),
					'kwh_awal_meter' => $this->input->post('kwh_awal_meter'),
					'kwh_akhir_meter' => $this->input->post('kwh_akhir_meter'),
					'kwh_terpakai' => $this->input->post('kwh_terpakai'),
					'tarif_per_kwh' => $this->input->post('tarif_per_kwh'),
					'jumlah_pemakaian' => $this->input->post('jumlah_pemakaian'),
					'mcb' => $this->input->post('mcb'),
					'id_bulan' => $this->input->post('id_bulan'),
					'id_pemakaian' => $this->input->post('id_pemakaian'),
					'biaya_beban' => $this->input->post('biaya_beban'),
					'jumlah_biaya_beban' => $this->input->post('jumlah_biaya_beban'),
					'total_tagihan' => $this->input->post('total_tagihan'),
					'no_bukti_pembayaran' => $this->input->post('no_bukti_pembayaran'),
					'tgl_bayar' => $this->input->post('tgl_bayar'),
					'status_bayar' => $this->input->post('status_bayar')
					);

					$data['tgl_bayar']	= ($data['tgl_bayar'] == '' ? NULL : date_format3($data['tgl_bayar'], 'YYYY-MM-DD'));
				
				// Set Primary Key Variable
				$no_tagihan_listrik = $data['no_tagihan_listrik'];
				$nama_tenant = $data['nama_tenant'];

				if ($this->input->post('status') == "0"){
					$DataStatus = array('status' => '1');
					$this->Tagihan->UpdateStatus($DataStatus, array('id_pemakaian' => $this->input->post('id_pemakaian')));
				}


				if (post("mode") == "create")
				{
					$this->Tagihan->Add($data);

					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					
					$this->Tagihan->Update($data, array('no_tagihan_listrik'	=> $data['no_tagihan_listrik']));

					$ls_response = "Data Tersimpan [edited]";
				}
			
				
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}




	function ajax_delete()
	{
		$ls_result	= "";
		$pesan	=	"";
		$no_tagihan_listrik	= get('no_tagihan_listrik');
		
		if ($no_tagihan_listrik != "")
		{
			// Delete Single
			$arr_rule	= array('no_tagihan_listrik' => $no_tagihan_listrik);
			$ls_result = $this->Tagihan->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('no_tagihan_listrik' => $row['no_tagihan_listrik']);
				$ls_result .= $this->Tagihan->DeleteSingle($arr_rule);
			}
		}
		
		echo $pesan;
		echo $ls_result;
	}
}
?>